package com.soxing.braindata.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.soxing.braindata.common.util.WebHelper;
import com.soxing.braindata.dao.entity.Equipment;
import com.soxing.braindata.dao.service.IEquipmentService;
import com.soxing.braindata.domain.request.EquipmentSearchRequest;
import com.soxing.braindata.domain.vo.EquipmentVo;
import com.soxing.braindata.service.manager.IEquipmentManager;
import com.soxing.common.utils.response.MyPage;
import com.soxing.common.utils.response.WebResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author :durango
 * @date :2022/6/11 9:46
 * @description 设备控制器
 */
@Slf4j
@RestController
@RequestMapping("/equipment")
@Api(value = "设备管理", tags = {"设备管理"})
public class EquipmentController {

    @Resource
    private IEquipmentManager equipmentManager;
    @Resource
    private IEquipmentService equipmentService;

    @PostMapping
    @ApiOperation(value = "新增设备")
    public WebResponse<Boolean> add(@RequestBody Equipment equipment) {
        return equipmentManager.add(equipment);
    }

    /**
     * 更新设备
     *
     * @param equipment
     * @return 返回操作结果 true:成功 false:失败
     */
    @PutMapping
    @ApiOperation(value = "更新设备")
    public WebResponse<Boolean> update(Equipment equipment) {
        if (equipment.getId() == null) {
            return WebResponse.fail("主键ID为空");
        }
        return equipmentManager.update(equipment);
    }

    /**
     * 基于id获取设备信息
     *
     * @param id
     * @return 返回设备信息
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "基于id获取设备信息")
    public WebResponse<EquipmentVo> getById(@PathVariable("id") Integer id) {
        return equipmentManager.getVoById(id);
    }

    /**
     * 基于id删除设备记录
     *
     * @param id 主键ID
     * @return 返回操作结果 true:成功 false:失败
     */
    @PostMapping("/delete/{id}")
    @ApiOperation(value = "基于id删除设备记录")
    public WebResponse<Boolean> deleteById(@PathVariable("id") Integer id) {
        return equipmentManager.deleteById(id);
    }

    /**
     * 基于条件查询设备
     *
     * @param equipmentSearchRequest
     * @return
     */
    @PostMapping("/search")
    @ApiOperation(value = "基于条件查询设备")
    public WebResponse<MyPage<EquipmentVo>> search(@RequestBody(required = false) EquipmentSearchRequest equipmentSearchRequest) {
        if (equipmentSearchRequest == null) {
            equipmentSearchRequest = new EquipmentSearchRequest();
        }
        return equipmentManager.search(equipmentSearchRequest);
    }

    /**
     * 获取站点所有设备
     *
     * @return
     */
    @GetMapping("/getAllEquipment")
    @ApiOperation(value = "获取所有设备")
    public WebResponse<List<Equipment>> getAllEquipment() {
        Date now = new Date();
        List<Equipment> list = equipmentService.list(new LambdaQueryWrapper<Equipment>().eq(Equipment::getSitePkId, WebHelper.getSitePkId()));
        return WebResponse.success(list);
    }


    @GetMapping("/getAllEquipmentByUser")
    @ApiOperation(value = "获取所在中心的所有设备")
    public WebResponse<List<Equipment>> getAllEquipmentByUser() {

        List<Equipment> list = equipmentService.list(new LambdaQueryWrapper<Equipment>().eq(Equipment::getSitePkId, WebHelper.getSitePkId()));
        return WebResponse.success(list);
    }


    @GetMapping("/getEquipmentByUser")
    @ApiOperation(value = "通过用户名获取设备")
    public WebResponse<MyPage<EquipmentVo>> getEquipmentByUser(@RequestParam String userName,@RequestParam int pageSize,@RequestParam int pageNum)
    {
        return equipmentManager.getEquipmentByUser(userName,pageSize,pageNum);
    }

}
